package cn.sccl.common.util;

import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;

import java.util.ArrayList;
import java.util.List;

public class PropertiesUtil {
    // private static final String DATA_SOURCE = "dataSource";
    private static CompositeConfiguration config;
    private static PropertiesConfiguration propertiesConfig;
    // private static DatabaseConfiguration dbConfig;

    static {
        config = new CompositeConfiguration();
        if (propertiesConfig == null) {
            try {
                propertiesConfig = new PropertiesConfiguration("dirms-config.properties");
                propertiesConfig.setReloadingStrategy(new FileChangedReloadingStrategy());
                /*
                 * DataSource ds = (DataSource)
				 * ContextUtil.getBean(DATA_SOURCE); dbConfig = new
				 * DatabaseConfiguration(ds, "CONFIG_PROPERTIES", "PROP_KEY",
				 * "PROP_VALUE"); config.addConfiguration(dbConfig);
				 */
                config.addConfiguration(propertiesConfig);
            } catch (ConfigurationException e) {
                e.printStackTrace();
            }
        }
    }


    private PropertiesUtil() {
        // nothing
    }


    public static String getString(String propertyKey) {
        return config.getString(propertyKey);
    }


    public static String getString(String propertyKey, String defaultValue) {
        return config.getString(propertyKey, defaultValue);
    }


    public static int getInt(String propertyKey) {
        return config.getInt(propertyKey);
    }


    public static int getInt(String key, int defaultValue) {
        return config.getInt(key, defaultValue);
    }


    public static float getFloat(String propertyKey) {
        return config.getFloat(propertyKey);
    }


    public static float getFloat(String propertyKey, float defaultValue) {
        return config.getFloat(propertyKey, defaultValue);
    }


    public static boolean getBoolean(String propertyKey) {
        return config.getBoolean(propertyKey);
    }


    public static boolean getBoolean(String propertyKey, boolean defualtValue) {
        return config.getBoolean(propertyKey, defualtValue);
    }


    public static String[] getStringArray(String propertyKey) {
        return config.getStringArray(propertyKey);
    }


    public static List<String> getStringList(String propertyKey) {
        List<String> list = new ArrayList<String>();
        String[] strArr = getStringArray(propertyKey);
        for (String value : strArr) {
            list.add(value);
        }
        return list;
    }


    public static List getList(String propertyKey) {
        return config.getList(propertyKey);
    }

}


